package com.liusy.code.templates;

import java.util.List;
import com.liusy.code.bean.*;

public class JspAddTemplate
{
  protected static String nl;
  public static synchronized JspAddTemplate create(String lineSeparator)
  {
    nl = lineSeparator;
    JspAddTemplate result = new JspAddTemplate();
    nl = null;
    return result;
  }

  public final String NL = nl == null ? (System.getProperties().getProperty("line.separator")) : nl;
  protected final String TEXT_1 = NL + "<%@ page language=\"java\" contentType=\"text/html; charset=UTF-8\"" + NL + "\tpageEncoding=\"UTF-8\"%>" + NL + "<%@ page isELIgnored=\"false\"%>" + NL + "<%@ include file=\"/include.inc.jsp\"%>" + NL + "" + NL + "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">" + NL + "<html>" + NL + "<head>" + NL + "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">" + NL + "" + NL + "<script type=\"text/javascript\">" + NL + "\tvar buttonArray = [ ];" + NL + "\t" + NL + "\tif(\"${requestScope.type}\" == \"add\"){" + NL + "\t\tbuttonArray.push({" + NL + "\t\t\tname : '确定'," + NL + "\t\t\tdisabled : false," + NL + "\t\t\tcallback : function() {" + NL + "\t\t\t\treturn submitForm(\"web/";
  protected final String TEXT_2 = "/";
  protected final String TEXT_3 = "/save";
  protected final String TEXT_4 = ".action\");" + NL + "\t\t\t}" + NL + "\t\t});" + NL + "\t}else if(\"${requestScope.type}\" == \"edit\"){" + NL + "\t\tbuttonArray.push({" + NL + "\t\t\tname : '确定'," + NL + "\t\t\tdisabled : false," + NL + "\t\t\tcallback : function() {" + NL + "\t\t\t\treturn submitForm(\"web/";
  protected final String TEXT_5 = "/";
  protected final String TEXT_6 = "/update";
  protected final String TEXT_7 = ".action\");" + NL + "\t\t\t}" + NL + "\t\t});" + NL + "\t}" + NL + "\t" + NL + "\tbuttonArray.push({" + NL + "\t\tname : '取消'," + NL + "\t\tcallback : function() {" + NL + "\t\t\treturn true;" + NL + "\t\t}" + NL + "\t});" + NL + "" + NL + "\tfunction submitForm(url) {" + NL + "\t\tvar result = '';" + NL + "\t\t$(\"#";
  protected final String TEXT_8 = "Form\").ajaxSubmit({" + NL + "\t\t\ttarget : '#groupForm'," + NL + "\t\t\ttype : 'post'," + NL + "\t\t\tasync : false," + NL + "\t\t\turl : '<%=basePath%>/'+url," + NL + "\t\t\tbeforeSubmit : function(formData, jqForm, options){" + NL + "\t\t\t\tif(jqForm.valid()){" + NL + "\t\t\t\t\tjqForm.mask('表单提交中...');" + NL + "\t\t\t\t\treturn true;" + NL + "\t\t\t\t}else{" + NL + "\t\t\t\t\treturn false;" + NL + "\t\t\t\t}" + NL + "\t\t\t}," + NL + "\t\t\tsuccess : function(data){" + NL + "\t\t\t\t$(\"#";
  protected final String TEXT_9 = "Form\").unmask();" + NL + "\t\t\t\tif('fail' != data.msg && null != data.msg){" + NL + "\t\t\t\t\tresult = data.msg;" + NL + "\t\t\t\t}else{" + NL + "\t\t\t\t\tresult = '';" + NL + "\t\t\t\t}" + NL + "\t\t\t}," + NL + "\t\t\terror : function(s){" + NL + "\t\t\t\t$(\"#";
  protected final String TEXT_10 = "Form\").unmask();" + NL + "\t\t\t\tresult = '';" + NL + "\t\t\t}" + NL + "\t\t});" + NL + "\t\tif('' != result){" + NL + "\t\t\t$.dialog.tips('保存成功！');" + NL + "    \t\t$(\"#";
  protected final String TEXT_11 = "_gridList\").trigger(\"reloadGrid\");" + NL + "    \t\treturn true;" + NL + "\t\t}else{" + NL + "\t\t\t$.dialog.alert('保存失败！');" + NL + "\t\t\treturn false;" + NL + "\t\t}" + NL + "\t}" + NL + "    " + NL + "" + NL + "\t$(function() {" + NL + "\t\t$.topuiRender();" + NL + "\t\t$('#";
  protected final String TEXT_12 = "Form').validate({" + NL + "      //      rules : {" + NL + "      //          'xiaozu.mingcheng' : {required : true }" + NL + "      //      }," + NL + "      //      messages : {" + NL + "      //          'xiaozu.mingcheng' : {required : '名称不能为空'}" + NL + "      //      }" + NL + "        });" + NL + "\t\t" + NL + "\t\t//查看" + NL + "\t\tif(\"${requestScope.type}\" == \"show\"){" + NL + "\t\t\t$(\"input,textarea\").attr(\"readonly\",\"readonly\");" + NL + "\t\t}" + NL + "\t});" + NL + "</script>" + NL + "" + NL + "</head>" + NL + "" + NL + "<body>" + NL + "\t<%@ include file=\"/web/";
  protected final String TEXT_13 = "/form/";
  protected final String TEXT_14 = "Form.form\"%>" + NL + "</body>" + NL + "</html>";
  protected final String TEXT_15 = NL;

  public String generate(Object argument)
  {
    final StringBuffer stringBuffer = new StringBuffer();
    
	JspTemplateArgs args = (JspTemplateArgs)argument;
	
	String modeName = args.getModeName();
	String entityName = args.getEntityName();
	String jspName = args.getJspName();
	
	List<EntityFieldInfo> entityFieldInfoList = args.getEntityFieldInfoList();
	
	String entityPackageName = args.getPackageNameInfo().getEntityPackageName();
	String daoPackageName = args.getPackageNameInfo().getDaoPackageName();
	String servicePackageName = args.getPackageNameInfo().getServicPackageName();
	String actionPackageName = args.getPackageNameInfo().getActionPackageName();
	
	String entityClassName = args.getClassNameInfo().getEntityClassName();
	String daoClassName = args.getClassNameInfo().getDaoClassName();
	String serviceClassName = args.getClassNameInfo().getServicClassName();
	String actionClassName = args.getClassNameInfo().getActionClassName();
	
	String entityObjectName = args.getClassNameInfo().getEntityObjectName();
	String daoObjectName = args.getClassNameInfo().getDaoObjectName();
	String serviceObjectName = args.getClassNameInfo().getServicObjectName();
	String actionObjectName = args.getClassNameInfo().getActionObjectName();
	String jsCode="";
	StringBuffer temp = new StringBuffer();
	for(EntityFieldInfo entityFieldInfo : entityFieldInfoList)
	{
		List<String> fieldNames = entityFieldInfo.getFieldNames();
		
		for(String fieldName : fieldNames)
		{
			temp.append(fieldName+":\"required\",");		
		}
	}
	jsCode = temp.substring(0,temp.length()-1);

    stringBuffer.append(TEXT_1);
    stringBuffer.append(modeName);
    stringBuffer.append(TEXT_2);
    stringBuffer.append(entityObjectName);
    stringBuffer.append(TEXT_3);
    stringBuffer.append(entityClassName);
    stringBuffer.append(TEXT_4);
    stringBuffer.append(modeName);
    stringBuffer.append(TEXT_5);
    stringBuffer.append(entityObjectName);
    stringBuffer.append(TEXT_6);
    stringBuffer.append(entityClassName);
    stringBuffer.append(TEXT_7);
    stringBuffer.append(entityObjectName);
    stringBuffer.append(TEXT_8);
    stringBuffer.append(entityObjectName);
    stringBuffer.append(TEXT_9);
    stringBuffer.append(entityObjectName);
    stringBuffer.append(TEXT_10);
    stringBuffer.append(entityObjectName);
    stringBuffer.append(TEXT_11);
    stringBuffer.append(entityObjectName);
    stringBuffer.append(TEXT_12);
    stringBuffer.append(modeName);
    stringBuffer.append(TEXT_13);
    stringBuffer.append(entityObjectName);
    stringBuffer.append(TEXT_14);
    stringBuffer.append(TEXT_15);
    return stringBuffer.toString();
  }
}
